﻿namespace Dotnet9.Infrastructure.EFCore;

public static class MediatorExtensions
{
    /// <summary>
    ///     把rootAssembly及直接或者间接引用的程序集（排除系统程序集）中的MediatR 相关类进行注册
    /// </summary>
    /// <param name="services"></param>
    /// <param name="rootAssembly"></param>
    /// <returns></returns>
    public static IServiceCollection AddMediatR(this IServiceCollection services, IEnumerable<Assembly> assemblies)
    {
        return services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(assemblies.ToArray()));
    }

    public static async Task DispatchDomainEventsAsync(this IMediator mediator, DbContext ctx)
    {
        var domainEntities = ctx.ChangeTracker
            .Entries<IDomainEvents>()
            .Where(x => x.Entity.GetDomainEvents().Any());

        var domainEvents = domainEntities
            .SelectMany(x => x.Entity.GetDomainEvents())
            .ToList(); //加ToList()是为立即加载，否则会延迟执行，到foreach的时候已经被ClearDomainEvents()了

        domainEntities.ToList()
            .ForEach(entity => entity.Entity.ClearDomainEvents());

        foreach (var domainEvent in domainEvents)
        {
            await mediator.Publish(domainEvent);
        }
    }
}